﻿
//-----------------------------------------------
///-----------------------------------------------
//首页JS
//------------------------------------------------
//------------------------------------------------




//获取全部餐厅信息以及分页信息
app.controller('HomeCtrl', ['$scope', '$http', '$location', 'HomeService', '$window', function ($scope, $http, $location, HomeService, $window) {
    //$scope.yzm = $location.absUrl().substring($location.absUrl().lastIndexOf("=") + 1);
    //console.log($location);
    //console.log($scope.yzm);
    //$scope.PostInfo = {
    //    YZM: $scope.yzm
    //}
    //if ($scope.yzm != null || $scope.yzm != "") {
    //    $http.post('/Home/UserLoginByYzm', $scope.PostInfo).success(function (resp) {
    //        console.log(resp);
    //        if (resp != 1) {
    //            console.log(true);
    //            $window.location.href = "/Home/Unusual";
    //        }
    //    }).error(function (resp) {
    //        console.log(resp);
    //    });
    //}
    $scope.SearchWord = {
        KeyWord: ''
    };
    
    ////查找商户
    //$scope.search = function (u) {
    //    $location.path('/Home/index');
    //    $location.search({ keyword: u });
    //    window.location.reload();//刷新页面，如果页面不刷新页面内容将不更改
    //}
    ////打开商户界面
    //$scope.openShop = function (u) {
    //    $location.path('/Shop/index');
    //    $location.search({ shopnumber: u });
    //    window.location.reload();
    //}
    //配置分页基本参数
    $scope.paginationConf = {
        currentPage: 1,//当前页
        itemsPerPage: 20,//每页显示的数量
        KeyWord: '',
        FenLei: 0,
        PaiXu: 0,
        DelMaxMon: 0,
        DelFormMon: 0
    };
    $scope.chkPaiXu = '0';
    $scope.chkMaxMon = '0';
    //$scope.selectedSite = '0';
   //$scope.selected = '0';
    $scope.chkFormMon = false;
    //餐厅分类
    $scope.types = [];
    $http.post("/Home/getDict")
        .success(function (resp) {
            $scope.types = resp;
            //console.log(resp);
        })
        .error(function (resp) {
            //alert(resp);
            console.log(resp);
        });
    var GetAllEmployee = function () {
        //搜索
        //$scope.paginationConf.KeyWord = $location.path().substr(1);
        //if ($location.search().keyword) {
        //    alert("+" + $location.search().keyword);
        //    $scope.paginationConf.KeyWord = $location.search().keyword;
        //} else {
        //    $scope.paginationConf.KeyWord = '';
        //}
        $scope.seach = function (u) {
            console.log(u);
            if (u) {
                $scope.paginationConf.KeyWord = u;

            }
            else {
                
                $scope.paginationConf.KeyWord = "";
            }
        }
       
        //排序

        $scope.selectPaiXu = function (u) {
            
            $scope.paginationConf.PaiXu = u;  
        }
        //分类
        $scope.selectType = function (u) {
            $scope.paginationConf.FenLei = u;
        }
        //起送价
        $scope.selectMaxMon = function (u) {
            
            if (u == "1") {
                //alert(u);
                $scope.paginationConf.DelMaxMon = 1;
            } else if (u == "2") {
                //alert(u + "+");
                $scope.paginationConf.DelFormMon = 1;
            
            } else {
                //alert(  "++");
                $scope.paginationConf.DelMaxMon = 0;
                $scope.paginationConf.DelFormMon = 0;                
            }
        }
        ////配送费
        //$scope.selectFormMon = function (u) {
        //    alert(u+"");
        //    if (u) {
        //        $scope.paginationConf.DelFormMon = 1;
        //    } else {
        //        $scope.paginationConf.DelFormMon = 0;
        //    }
        //}

        var postData = {
            pageIndex: $scope.paginationConf.currentPage,
            pageSize: $scope.paginationConf.itemsPerPage,
            KeyWord: $scope.paginationConf.KeyWord,
            FenLei: $scope.paginationConf.FenLei,
            PaiXu: $scope.paginationConf.PaiXu,
            DelMaxMon: $scope.paginationConf.DelMaxMon,
            DelFormMon: $scope.paginationConf.DelFormMon
        }
        
        HomeService.shoplist(postData).success(function (response) {
            //$scope.paginationConf.totalItems = response.total;
            $scope.total = response.total;
            $scope.shops = response.rows;
            //console.log($scope.shops);
        }).error(function (resp) { console.log(resp); });
    }
    /***************************************************************
   当页码和页面记录数发生变化时监控后台查询
   如果把currentPage和itemsPerPage分开监控的话则会触发两次后台事件。 
   ***************************************************************/
    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage + paginationConf.DelMaxMon + paginationConf.DelFormMon + paginationConf.FenLei + paginationConf.PaiXu + paginationConf.KeyWord', GetAllEmployee); //



}]);

app.factory('HomeService', ['$http', function ($http) {
    var typelist = function () {
        return $http.post('/Home/getDict');
    }
    var shoplist = function (postData) {
        return $http.post('/Home/getAllShopInfo', postData);
    }
    return {
        typelist: function () {
            return typelist();
        },
        shoplist: function (postData) {
            return shoplist(postData);
        }
    }
}]);

app.factory('ShopService', ['$http', function ($http) {
    var list = function (postShopNum) {
        return $http.post('/Shop/Shop', postShopNum);
    }
    var eva = function (dishid) {
        $http.post('/Shop/getDishEva', dishid);
    }
    return {
        list: function (postShopNum) { return list(postShopNum); },
        eva: function (dishid) { return eva(dishid); }
    }
}]);

app.factory('isLoginService', ['$http', function ($http) {
    var list = function () {
        return $http.post('/Home/isLogin');
    }
    return {
        list: function () {
            return list();
        }
    }
}]);

app.factory('EvaService', ['$http', function ($http) {
    var eva = function (dishid) {
        return $http.post('/Shop/getDishEva', dishid);
    }
    return {
        eva: function (dishid) { return eva(dishid) }
    }
}]);